package com.todo.event.activity.application.handler.activityreward;

import com.alibaba.fastjson.JSONObject;
import com.todo.event.activity.application.entity.context.RewardSendContext;
import com.todo.event.activity.common.enums.EventActivityExecuteRewardEnum;
import lombok.extern.slf4j.Slf4j;

/**
 * @author azhebuxing
 * @date 2025/1/13 19:50
 * @description
 */
@Slf4j
public abstract class AbstractRewardHandle {

    public boolean handle(RewardSendContext context) {
        if (context == null) {
            return true;
        }
        if (match(context)) {
            boolean notNeedNextHandle = preHandle(context);
            //前置操作成功则无需后续逻辑
            if (notNeedNextHandle) {
                log.info("AbstractRewardHandle#handle preHandle success reward:【{}】 context:【{}】", getReward(), JSONObject.toJSONString(context));
                return true;
            }
            try {
                Boolean result = doHandle(context);
                log.info("AbstractRewardHandle#handle execute success reward:【{}】 context:【{}】 result:【{}】", getReward(), JSONObject.toJSONString(context), result);
                return result;
            } catch (Exception ex) {
                log.error("AbstractRewardHandle#handle execute error reward:【{}】 context:【{}】", getReward(), JSONObject.toJSONString(context), ex);
            }
        }
        return true;
    }

    //match 暂时默认成功，后续可能存在对应的逻辑需要进行判断
    public boolean match(RewardSendContext context) {
        return true;
    }

    protected abstract boolean preHandle(RewardSendContext context);

    protected abstract Boolean doHandle(RewardSendContext context);

    protected abstract EventActivityExecuteRewardEnum getReward();
}
